<?php
/**
 * Ecart
 * 
 * This file is part of Ecart.
 * 
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @category    Ecart
 * @package     Ecart_Admin
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

/**
 * 
 * @category    Ecart
 * @package     Ecart_Admin
 * @subpackage  Controller
 * @author      Ecart Core Team <core@ecartcommerce.com>
 */
class Ecart_Admin_Template_LayoutController extends Ecart_Admin_Controller_Back
{
    /**
     * @var Ecart_Core_Model_Template_Layout_Page
     */
    protected $_table;

    private $_templateId;

    public function init()
    {
        parent::init();
        $this->getHelper('layout')->disableLayout();
        $this->_table = Ecart::single('core/template_layout_page');
        $this->_templateId = (int) $this->_getParam('tId', 0);
    }

    public function listAction()
    {
        $where = 'template_id = ' . $this->_templateId;

        $data = $this->_table->fetchAll($where)->toArray();
        
        return $this->_helper->json->sendSuccess(array(
            'data' => array_values($data)
        ));
    }

    public function listCollectAction()
    {
        $layouts = Ecart_Collect_Layout::collect();
        
        $result = array();
        $i = 0;
        foreach ($layouts as $layout) {
            $result[$i]['name'] = $layout;
            $i++;
        }
        
        return $this->_helper->json->sendSuccess(array(
            'data' => $result
        ));
    }

    public function saveAction()
    {
        $this->_helper->layout->disableLayout();
        
        $data = Zend_Json::decode($this->_getParam('data'));
        
        return $this->_helper->json->sendJson(array('success' =>
            Ecart::single('core/template_layout_page')->save(
                $this->_templateId, $data
            )
        ));
    }
    
    public function deleteAction()
    {
        $ids = Zend_Json::decode($this->_getParam('data'));
        
        if (!count($ids)) {
            Ecart::message()->addError(
                Ecart::translate('admin')->__(
                    'No data to delete'
                )
            );
            return $this->_helper->json->sendFailure();
        }
        $this->_table->delete($this->db->quoteInto('id IN(?)', $ids));
        
        Ecart::message()->addSuccess(
            Ecart::translate('admin')->__(
                'Data was deleted successfully'
            )
        );
        return $this->_helper->json->sendSuccess();
    }
}